
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter_module/request/NetRequest.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'userInfo.dart';
import 'package:flutter_module/tool/fileManager.dart';
import 'package:flutter_module/tabbar/routers/application.dart';
import 'package:flutter_module/tabbar/routers/Routes.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter_module/tool/userManager.dart';

  class LoginScreen extends StatefulWidget {
    @override
    _LoginScreenState createState() => _LoginScreenState();
  }
  
  class _LoginScreenState extends State<LoginScreen> {

    bool _isShowLoading = false;

    @override
    Widget build(BuildContext context) {
      return Scaffold(
      appBar: AppBar(
        title: Text('登录'),
      ),
      body: Center(
        child: Stack(
          children: <Widget>[
            Center(
              child: RaisedButton(
                child: Text('登录'),
                onPressed: (){
                  setState(() {
                    _isShowLoading = true;
                  });
                  request().then((val){
                    setState(() {
                      _isShowLoading = false;
                    });
                    print(val);
                    FileManager.saveUserInfo(val);
                    UserManager().userInfo = UserInfo.fromJson(val);
                    Application.router.navigateTo(context, Routes.root,transition: TransitionType.native,clearStack: true);
                  }).catchError((error){
                    setState(() {
                      _isShowLoading = false;
                    });
                    print(error);
                  });
                  
                },
              ),
            ),
            Offstage(
              offstage: !_isShowLoading,
              child: SpinKitCircle(color: Colors.lightBlue,),
            )
          ],
        ),
      )
    );
  }

  Future request() async {

    Response response;
    NetRequest dio = NetRequest(false);
    String url = 'oauth/token';
    Map pam = {'enterpriseCode':'yunding_ent_test','account':'joy3','password':'wflYk5saIuyUdCjuOkX6NQ=='};
    response = await dio.post(url,data: pam);
    return response.data;
  }

}